package MIU.TwistedLabirynth.menu;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;

import MIU.TwistedLabirynth.R;
import MIU.TwistedLabirynth.db.HighScoreDb;
import MIU.TwistedLabirynth.pac.GameLogicController;

public class MainMenu {

	private final GameLogicController gameLogicController;
	private Scene quitScene;

	private BitmapTextureAtlas mMenuTexture;
	private TextureRegion mMenuPlayTextureRegion;
	private TextureRegion mMenuQuitTextureRegion;
	private TextureRegion mMenuSettingsTextureRegion;
	private TextureRegion mMenuHighScoreTextureRegion;
	
	private BitmapTextureAtlas mSubMenuTexture;
	private TextureRegion mMenuOkTextureRegion;
	private TextureRegion mMenuBackTextureRegion;

	public MainMenu(GameLogicController gameLogicController) {
		this.gameLogicController = gameLogicController;
	}

	public void loadResources() {
		this.mSubMenuTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mMenuOkTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mSubMenuTexture,
				gameLogicController, "menu_black_button_ok.png", 0, 0);
		this.mMenuBackTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mSubMenuTexture,
				gameLogicController, "menu_black_button_back.png", 0, 50);

		this.mMenuTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mMenuPlayTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTexture,
				gameLogicController, "menu_black_button_play.png", 0, 0);
		this.mMenuQuitTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTexture,
				gameLogicController, "menu_black_button_quit.png", 0, 50);
		this.mMenuSettingsTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTexture,
				gameLogicController, "menu_black_button_settings.png", 0, 100);
		this.mMenuHighScoreTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTexture,
				gameLogicController, "highscore.png", 0, 150);
		gameLogicController.getEngine().getTextureManager().loadTextures(this.mMenuTexture, this.mSubMenuTexture);
	}

	public Scene createMenuScene() {
		if (quitScene == null) {
			quitScene = createQuitScene();
		}

		Scene scene = new Scene();
		Sprite buttonLevel = new Sprite(100, 5, 250, 70, this.mMenuPlayTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (gameLogicController.checkTouchTime()) {
					gameLogicController.setLevelMenuScene();
				}
				return true;
			}
		};
		buttonLevel.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		scene.registerTouchArea(buttonLevel);
		scene.attachChild(buttonLevel);

		buttonLevel = new Sprite(100, 85, 250, 70, this.mMenuSettingsTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (gameLogicController.checkTouchTime()) {
					gameLogicController.setSettingsMenuScene();
				}
				return true;
			}
		};
		buttonLevel.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		scene.registerTouchArea(buttonLevel);
		scene.attachChild(buttonLevel);

		buttonLevel = new Sprite(100, 165, 250, 70, this.mMenuHighScoreTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (gameLogicController.checkTouchTime()) {
					AlertDialog.Builder alertbox = new AlertDialog.Builder(gameLogicController);
					alertbox.setTitle("HIGHSCORE");
					alertbox.setIcon(R.drawable.trophy);
					alertbox.setCancelable(false);
					StringBuilder str = new StringBuilder();
					Cursor allRows = gameLogicController.getHighscoreDB().GetAllRows();

					for (int i = 0; i < allRows.getCount(); i++) {
						allRows.moveToNext();
						if (i != 9) {
							str.append(i + 1 + ".  " + allRows.getString(HighScoreDb.NAME_COLUMN) + "  ("
									+ allRows.getString(HighScoreDb.NUMBER_COLUMN) + " points)\n");
						} else {
							str.append(i + 1 + ".  " + allRows.getString(HighScoreDb.NAME_COLUMN) + "  ("
						+ allRows.getString(HighScoreDb.NUMBER_COLUMN) + " points)\n");
						}
					}
					alertbox.setMessage(str.toString());
					alertbox.setPositiveButton("Close", // adds a close button

							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog, int id) {

								}

							});
					alertbox.show();
				}
				return true;
			}
		};
		buttonLevel.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		scene.registerTouchArea(buttonLevel);
		scene.attachChild(buttonLevel);

		buttonLevel = new Sprite(100, 245, 250, 70, this.mMenuQuitTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (gameLogicController.checkTouchTime()) {
					gameLogicController.getEngine().setScene(quitScene);
				}
				return true;
			}
		};
		buttonLevel.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		scene.registerTouchArea(buttonLevel);
		scene.attachChild(buttonLevel);

		return scene;
	}

	private Scene createQuitScene() {
		Scene scene = new Scene();
		Sprite buttonLevel = new Sprite(100, 100, 250, 70, this.mMenuOkTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (gameLogicController.checkTouchTime()) {
					gameLogicController.getPlayerProfileManager().saveBeforeQuit();
					gameLogicController.finish();
				}
				return true;
			}
		};

		scene.registerTouchArea(buttonLevel);
		scene.attachChild(buttonLevel);

		buttonLevel = new Sprite(100, 170, 250, 70, this.mMenuBackTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (gameLogicController.checkTouchTime())
					gameLogicController.getEngine().setScene(createMenuScene());
				return true;
			}
		};
		scene.registerTouchArea(buttonLevel);
		scene.attachChild(buttonLevel);
		return scene;
	}

}
